package com.example.teesystem.controller;

import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.exam.ExamInsertVo;
import com.example.teesystem.entity.vo.exam.ExamOutPutVo;
import com.example.teesystem.service.ExamService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/4/15 14:59
 */
@RestController
@RequestMapping(value = "/tee/exam" , produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor
public class ExamController {

    private final ExamService examService;

    /**
     * 获取考试试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/obtainExaminationPaper")
    public Response obtainExaminationPaper(@RequestBody Map<String, String> map){
        int paperId;
        try{
            paperId = Integer.parseInt(map.get("paperId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return examService.obtainExaminationPaper(paperId);
    }

    /**
     * 考试答题(自动提交)
     * @param examInsertVo
     * @return
     */
    @PostMapping(value = "/answerExam")
    public Response answerExam(@Valid @RequestBody ExamInsertVo examInsertVo){
        return examService.addExamAnswer(examInsertVo);
    }

    /**
     * 提交答题结果（手动提交）
     * @param examInsertVo
     * @return
     */
    @PostMapping(value = "/submitAnswer")
    public Response submitAnswer(@Valid @RequestBody ExamInsertVo examInsertVo){
        return examService.submitExamAnswer(examInsertVo);
    }

    /**
     * 恢复已答试题
     * @param map
     * @return
     */
    @PostMapping(value = "/recoveryExamIssue")
    public Response recoveryExamIssue(@RequestBody Map<String, String> map){
        int paperId ;
        try {
            paperId = Integer.parseInt(map.get("paperId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examService.recoveryExamIssue(paperId);
    }

    /**
     * 导出试卷
     * @param examOutPutVo
     */
    @PostMapping(value = "/outPutExamPaper")
    public void outPutExamPaper(@Valid @RequestBody ExamOutPutVo examOutPutVo){
        examService.getExamPaperSimpleMessage(examOutPutVo.getPaperId(), examOutPutVo.getIssueList());
    }
}
